06. Exercise 1: Load Image

Summary & Exercise Instructions

Exercise 1: Load and display an image

In this exercise, you will get introduced to a library called pydicom, which we will use throughout the course to manipulate DICOM images. You will load a single DICOM file representing a slice of a 3D volume, apply a window and level transform and save it as a png image.

You will need to try to match the WW/WL setting as close as possible to what you will see when you load this image into a viewer.

You can use the online viewer at ViewMyScans or you can install Microdicom and load the image locally.

You can use pydicom library reference to look up methods and parameters, but we will tell you what methods to use.

Pydicom loads the image into an object that intercepts calls to its fields and tries to look up this field in the list of DICOM tags that have been loaded. Tags can be either read by using semantic names as they are defined in the DICOM standard (collapsing spaces), e.g.:
image.SOPClassUID or image.SeriesDescription

Or by referencing the tag’s (Group ID, Element ID) format, e.g.:

Image.[0x0028,0x0030].value

You can access pixel data of the image by calling on property pixel_array which returns a NumPy array with pixels.

We will use the library called PIL in order to save the image as PNG. To write an array using PIL, make sure to tell it to write it as 8bit grayscale by using mode attribute like so:

im = Image.fromarray(out, mode="L")

Remember that L mode expects that your array contains 8bit unsigned integers!

Bonus points if you can apply the windowing process so that image appears exactly the same way as in your viewer. You would need to use Microdicom or Slicer for that as it will show you the exact window width and level values. If you are using Udacity Workspaces, you can switch to Desktop and find a link to 3D slicer there.

Udacity Workspace Note: This workspace will include the ability to access a virtual desktop via the "Go to Desktop" button in which you will be able to access the Slicer tool - you can find a shortcut on the desktop.

Code

If you need a code on the https://github.com/udacity.

  • userCode:

    /root/miniconda3/bin/conda init bash > /dev/null
    grep -qxF "conda activate medai" /root/.bashrc || echo "conda activate medai" >> /root/.bashrc

  • Exercise: quiz

    QUESTION:

    Based on the DICOM metadata, what was the imaging modality used to create the image?

    SOLUTION:

    These answers need to be solved by yourself, I believe you can do it

    Exercise 1 Solution

    You can find the solution here.

    Feel free to browse through it, and pay attention to how the windowing operation was applied and how Pillow library was used to convert the voxel array into an image.